define(function(require, exports, module) {
	//提示语
	var hint = function(str) {
		return "<span class='input-hint js-input-hint'>" + str + "</span>";
	}

	//显示提示语
	var show = function($this, hint) {
		$this.closest('div').css('position', 'relative');
		$this.siblings("span.js-input-hint").remove();
		$this.after(hint);
	}

	//删除提示语
	var remove = function($this) {
		$this.siblings("span.js-input-hint").remove();
	}
	
	var is_data = function($this, verify, value){
	if (verify.indexOf("date") == 0) {
			re = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;
			if (re.test(value)) {
				remove($this);
				return true;
			} else {
				show($this, hint('必须输入时间格式 如：2015-10-10'));
				return false;
			}
		} else {
			return true;
		}
	}


	//数字验证
	var is_number = function($this, verify, value) {
		if (verify.indexOf("number") == 0) {
			re = /^[0-9]*[1-9][0-9]*$/;
			if (re.test(value)) {
				remove($this);
				return true;
			} else {
				show($this, hint('必须为整数'));
				return false;
			}
		} else {
			return true;
		}
	}
	
	//是否是电话号码
	var is_phone =function($this, verify, value){
		if (verify.indexOf("phone") == 0) {
			re = /1[3458]{1}\d{9}$/;
			if (re.test(value)) {
				remove($this);
				return true;
			} else {
				show($this, hint('请输入电话号码'));
				return false;
			}
		} else {
			return true;
		}
	}

	//长度验证
	var is_max_length = function($this, verify, value) {
		//判断字符串的最大长度
		if (verify.indexOf("max_") == 0) {
			var len = verify.substr(4);
			if (value.length > len) {
				show($this, hint('长度不能超过' + len + '位'));
				return false;
			} else {
				remove($this);
				return true;
			}
		} else {
			return true;
		}
	}

	var is_min_length = function($this, verify, value) {
		//判断字符串的最大长度
		if (verify.indexOf("min_") == 0) {
			var len = verify.substr(4);
			if (value.length < len) {
				show($this, hint('长度至少' + len + '位'));
				return false;
			} else {
				remove($this);
				return true;
			}
		} else {
			return true;
		}
	}


	var is_email = function(str) {
		re = "/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/";
		return re.test(text) ? true : false;
	}

	var is_not_empty = function($this, verify, value) {
		//判断字符串的最大长度
		if (verify.indexOf("empty") == 0) {
			if ($this.attr('type') == 'file') {
				if (value == '') {
					show($this, hint('还没有上传图片'));
					return false;
				} else {
					remove($this);
					return true;
				}
			} else {
				if (value == '') {
					show($this, hint('值不能为空'));
					return false;
				} else {
					remove($this);
					return true;

				}
			}
		} else {
			return true;
		}
	}

	var is_between = function($this, verify, value) {
		//判断字符串的最大长度
		if (verify.indexOf("between_") == 0) {
			//先判断是不是数字
			if (!isNaN(value)) {
				remove($this);
			} else {
				show($this, hint('必须为大于0的值'));
				return false;
			}

			var arr = verify.split("_");
			value = parseInt(value);
			if (arr[1] == '*' && arr[2] != '*') {
				if (arr[2] < value) {
					show($this, hint('值不能大于' + arr[2]));
					return false;
				} else {
					remove($this);
					return true;
				}
			} else if (arr[1] != '*' && arr[2] == '*') {
				if (arr[1] > value) {
					show($this, hint('值不能小于' + arr[1]));
					return false;
				} else {
					remove($this);
					return true;
				}
			} else if (arr[1] != '*' && arr[2] != '*') {
				if (arr[1] > value || arr[2] < value) {
					show($this, hint('值必须在' + arr[1] + '~' + arr['2'] + '之间'));
					return false;
				} else {
					remove($this);
					return true;
				}
			} else {
				return true;
			}
		} else {
			return true;
		}
	}

	$(document).on('blur', 'input[verify],select[verify]', function() {
		var $this = $(this);
		var verify = $this.attr('verify');
		var value = $(this).val();
		validator($this, verify, value);
	});



	$(document).on('change', "input[verify][type='file']", function(e) {
		var path_name = e.srcElement.value;
		var arr = path_name.split('\\');
		var file_name = arr[arr.length - 1];
		$(this).nextAll('span.filename').text(file_name);
		$(this).nextAll('span.action').text('已选图片');
		remove($(this));
	});

	//统一验证接口
	var validator = function($this, verify, value) {
		var arr = verify.split(",");
		for (var i = 0; i < arr.length; i++) {
			if (!is_not_empty($this, arr[i], value)) return false;
			if (!is_max_length($this, arr[i], value)) return false;
			if (!is_min_length($this, arr[i], value)) return false;
			if (!is_between($this, arr[i], value)) return false;
			if (!is_number($this, arr[i], value)) return false;
			if (!is_phone($this, arr[i], value)) return false;
			if (!is_data($this, arr[i], value)) return false;
		}
		return true;
	}


	var yun_form_validator = function($form) {
		var sign = true;
		$input = $form.find('input[verify],select[verify]');
		$input.each(function() {
			var $this = $(this);
			var verify = $this.attr('verify');
			var value = $(this).val();
			//验证每个输入框
			if (!validator($this, verify, value)) {
				sign = false;
			}
		});

		return sign;
	}

	/*监听表单提交按钮*/
	$(document).on('change', "[from-validator]", function(e) {
		return yun_form_validator($(this).closest('form'));
	});

	/*查找带回模块*/
	$(document).on('click', "[choose-back]", function(e) {
        //#123_value_123,.123_value_值
        var content = $(this).attr('choose-back');
        var item_arr = content.split(',');//获得需要显示的对象数组
        var len = item_arr.length;
        for(var i=0;i<len;i++){
        	//获得每一个对象
        	var item = item_arr[i].split('_');
        	$(item[0]).attr(item[1],item[2]);
        }
	});

	window.yun_form_validator = yun_form_validator;
	//格式验证（邮箱，电话，链接）

});